package net.sunzc.takephoto.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.YuvImage;

import java.io.ByteArrayOutputStream;

public class BitmapUtils {


    private static final String TAG = "BitmapUtils";

    /**
     * RGB32转灰度
     *
     * @param srcImgData 输入RGB32数据
     * @param nWidth
     * @param nHeight
     * @param pbGrayData 输出灰度数据
     */
    public static void getGrayDataFromRgb32(int[] srcImgData, int nWidth, int nHeight, byte[] pbGrayData) {
        Bitmap srcColor = Bitmap.createBitmap(srcImgData, nWidth, nHeight, Bitmap.Config.ARGB_8888);
        int w = srcColor.getWidth(), h = srcColor.getHeight();
        int[] pix = new int[w * h];
        srcColor.getPixels(pix, 0, w, 0, 0, w, h);

        int alpha = 0xFF << 24;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                // 获得像素的颜色
                int color = pix[w * i + j];
                int red = ((color & 0x00FF0000) >> 16);
                int green = ((color & 0x0000FF00) >> 8);
                int blue = color & 0x000000FF;
                color = (red + green + blue) / 3;
                color = alpha | (color << 16) | (color << 8) | color;
                pbGrayData[w * i + j] = (byte) color;
            }
        }
        pix = null;
        if (!srcColor.isRecycled()) {
            srcColor.recycle();
        }
    }

    //计算图片的缩放值
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    // 根据路径获得图片并压缩，返回bitmap用于显示
    public static Bitmap getSmallBitmap(String filePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int tempWidth = options.outWidth;
        int tempHeight = options.outHeight;
        while (tempWidth > 1024 || tempHeight > 1024) {
            tempWidth /= 2;
            tempHeight /= 2;
        }
        options.inSampleSize = calculateInSampleSize(options, tempWidth, tempHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    private static Bitmap getSmall100Bitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        options.inSampleSize = calculateInSampleSize(options, 100, 100);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    public static byte[] bitmapToByte(Bitmap photo, int quality) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, quality, stream);
        return stream.toByteArray();
    }

    public static byte[] getSmallBitmap(Bitmap photo) {
        MyLog.i(TAG, "缩小的图片" + photo);
        byte[] photoBytes = bitmapToByte(photo, 100);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        MyLog.i(TAG, "准备缩小图片的长度：" + photoBytes.length);
        BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length, opts);
        int width = opts.outWidth;
        int height = opts.outHeight;
        while (width > 480 || height > 320) {
            width /= 2;
            height /= 2;
        }
        MyLog.i(TAG, "比例计算" + width + "/" + height);
        opts.inSampleSize = calculateInSampleSize(opts, width, height);
        opts.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length, opts);
        MyLog.i(TAG, "缩放图片完成" + bitmap);
        byte[] smallBitmapByte = bitmapToByte(bitmap, 30);
        MyLog.i(TAG, "压缩图片完成:" + (smallBitmapByte.length / 1024) + "KB");
        return smallBitmapByte;
    }

    public static Bitmap getBitmap(byte[] yuvImageData, int width, int height, int format) {
        YuvImage yuv = new YuvImage(yuvImageData, format, width, height, null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
        byte[] bytes = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        MyLog.i(TAG, "拍照成功" + bytes.length);
        return bitmap;
    }
}
